/**
 * Created by tanhaican on 2016/12/10.
 */
define(function (require, exports, module) {
    var sTpl = require("templates/modules/parent/groupon_list.html"),
        Constants = require('js/common/Constants'),
        Utils = require('js/common/Utils'),
        getGrouponListURL = Constants.APP_SERVER_URL + 'group/search/findGroupGoodInfo',
        orgId,
        pageIndex = 1;// 当前页码

    require("js/lib/layer_mobile/layer.js");
    require("js/lib/layer_mobile/need/layer.css");

    var GrouponList = Vue.extend({
        template: sTpl,
        data: function() {
            return {
                grouponList: []
            }
        },
        ready: function () {
            this.fetchData(pageIndex);
        },
        route: {
            data: function(transition) {
                orgId = transition.to.params.orgId
            }
        },
        methods: {
            // 上拉加载更多数据
            fetchData: function (_pageIndex) {
                var vm = this;
                GrouponService.getGrouponList(orgId, _pageIndex, function(data) {
                    Utils.concatArr(vm.grouponList, data && data.rows || []);
                });
            },
            toGrouponDetail: function(uuid) {
                var vm = this;

                vm.$router.go({name: 'orgGrouponItemDetail', params: {uuid: uuid}});
            }
        }
    });

    var GrouponService = {
        getGrouponList: function (orgId, pageIndex, callback) {
            var param = {
                orgId: orgId,
                page: pageIndex || 1,
                rows: Constants.PAGE_INFO.PAGE_SIZE
            };
            var promise = Utils.post(getGrouponListURL, param);

            promise.then(function (response) {
                var retObj = response.body;

                if (retObj && retObj.code === 1) {
                    callback && callback(retObj.obj);
                } else {
                    layer.open({
                        time: 1,
                        skin: 'msg',
                        content: retObj.msg
                    });
                }
            }, function (response) {
                layer.open({
                    time: 1,
                    skin: 'msg',
                    content: '请求数据失败'
                });
            });
        }
    };


    module.exports = GrouponList;
});